/** @odoo-module **/
class cellEditor{
    // 创建编辑器后调用一次
    init(params){
        // console.log(params);
        let _this = this;
        this.params = params;
        this.owl = params.context.owl_widget;
        this.data = params.data;
        this.field = params.colDef.field;
        this.day = params.colDef.field.split('_')[0];
        this.dish_data = [];

        let value = params.value || '';

        this.oldValue = value;
        this.currentValue = value;

        let el = document.createElement('div');
        let _html = '';
        _html += `<select class="form-select form-select-sm resultData" name="${params.colDef.field}"></select>`
        el.innerHTML = _html;
        this.el = el;
        el.getElementsByTagName('select')[0].addEventListener('change', (event) => {
            // console.log(event);
            this.currentId = Number(event.target.value);
            try {
                this.currentValue = this.owl.index.plan_top_category.find(v=>v.id == this.currentId).name;
            }catch (e) {
                this.currentValue = '';
            }
            // this.data[this.day+'_limit_category_id'] = this.currentId;
            // this.owl.needShowTipCell = {
            //     id:this.data.id,
            //     day:this.day
            // }
        });
    }
    // 插入elementNode
    getGui(){
        return this.el;
    }
    // 将值发送到当前的表格
    getValue(){
        return this.currentValue
    }
    // 编辑开始前调用
    isCancelBeforeStart() {
        return false;
    }
    // 编辑结束后返回一次
    // 如果返回true,编辑结果失效
    isCancelAfterEnd(){
        if(this.currentValue == this.oldValue){
            return true;
        }else{
            return false;
        }
    }
    afterGuiAttached(){
        this.setResult();
    }
    isPopup(){
        return true
    }

    // getPopupPosition(){
    //     return 'under'
    // }
    setResult(){
        let _this = this;
        let el = this.el;
        let str = '';
        let index = this.owl.index;
        let dishTopCategoryIds = this.owl.dishTopCategoryIds;
        let dish_top_category_id = this.data.dish_top_category_id;
        let plan_top_category_ids = dishTopCategoryIds[dish_top_category_id]||[];

        // console.log(plan_top_category_ids);
        str += `<option value="">请选择</option>`;
        for (const item of index.plan_top_category) {
            if(plan_top_category_ids.includes(item.id)){
                if(_this.oldValue == item.name){
                    str += `<option value="${item.id}" selected="selected">${item.name}</option>`;
                }else{
                    str += `<option value="${item.id}">${item.name}</option>`;
                }
            }
        }
        el.getElementsByTagName('select')[0].innerHTML = str;
    }
}


export default cellEditor
